<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_doctors extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize()
	{
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		
		require_once('engine/include_dao.php');
		return true;
		
	}
	public function create_category($data)
	{
		$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->admin_id = $data['admin_id'];
		$doctorsCategory->category_name = $data['category_name'];
		$doctorsCategory->created_date = date("Y-m-d H:i:s");
		$doctorsCategory->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getDoctorsCategoryDAO()->insert($doctorsCategory);
		//print_r($result);exit;
		
	/* $query="insert into `doctors_category` (category_name,admin_id,created_date,updated_date) VALUES ('".$data['category_name']."','".$data['admin_id']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	 */
		//$this->db->last_query();exit;
	if($result !== 0){
			return array('message'=>'Category successfully created');			
		}else{
			return array('message'=>'Category Creation Failed');
		}	
	}
	public function create_doctor($data)
	{
		$doctor = new Doctor();
		$doctor->category_id = $data['category_id'];
		$doctor->doctor_name = $data['doctor_name'];
		$doctor->doctor_address = $data['doctor_address'];
		$doctor->doctor_city = $data['doctor_city'];
		$doctor->state_id = $data['state_id'];
		$doctor->doctor_zipcode = $data['doctor_zipcode'];
		$doctor->doctor_comments = $data['doctor_comments'];
		$doctor->created_date = date("Y-m-d H:i:s");
		$doctor->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getDoctorsDAO()->insert($doctor);
		//print_r($result);exit;
/* 	$query="insert into `doctors` (doctor_name,category_id,doctor_address,doctor_city,state_id,doctor_zipcode,doctor_comments,created_date,updated_date) VALUES ('".$data['doctor_name']."','".$data['category_id']."','".$data['doctor_address']."','".$data['doctor_city']."','".$data['state_id']."','".$data['doctor_zipcode']."','".$data['doctor_comments']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	 */
		//$this->db->last_query();exit;
	if($result !== 0){
			return array('message'=>'subModule successfully created');			
		}else{
			return array('message'=>'submodule Creation Failed');
		}	
	}
	public function get_doctorcategory_list()
	{	
		$doctorsCategory = new DoctorsCategory();
		$result = DAOFactory::getDoctorsCategoryDAO()->doctorcategory_list($doctorsCategory);
		//print_r($result);exit;
		/* $query = "select category_id,category_name from doctors_category";
		$result = $this->conn->dbMultiple($query);	 */	
		if($result !== ''){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}
	}
	public function get_doctorstates_list()
	{	
			$doctor = new Doctor();
		$result = DAOFactory::getDoctorsDAO()->doctorstates_list($doctor);
		//print_r($result);exit;
		
		/* $query = "select state_id,state_name from states";
		$result = $this->conn->dbMultiple($query); */		
		if($result){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}
	}
	public function edit_category($data)
	{
			$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->category_id = $data['category_id'];
		$doctorsCategory->admin_id = $data['admin_id'];
		$doctorsCategory->category_name = $data['category_name'];
		$doctorsCategory->status = $data['status'];
		$doctorsCategory->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getDoctorsCategoryDAO()->update($doctorsCategory);
		//print_r($result);exit;
		
	/* $query="update `doctors_category` set category_name='".$data['category_name']."',updated_date='".date("Y-m-d H:i:s")."' where category_id=".$data['category_id'];
		$result=$this->conn->dbCommand($query); */		
		if($result !== 0){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function edit_doctor($data)
	{
			$doctor = new Doctor();
		$doctor->doctor_id = $data['doctor_id'];
		$doctor->category_id = $data['category_id'];
		$doctor->doctor_name = $data['doctor_name'];
		$doctor->doctor_address = $data['doctor_address'];
		$doctor->doctor_city = $data['doctor_city'];
		$doctor->state_id = $data['state_id'];
		$doctor->doctor_zipcode = $data['doctor_zipcode'];
		$doctor->doctor_comments = $data['doctor_comments'];
		$doctor->status = $data['status'];
		$doctor->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getDoctorsDAO()->update($doctor);
		//print_r($result);exit;
		
		/* 	$query="update `doctors` set doctor_name='".$data['doctor_name']."',category_id='".$data['category_id']."',doctor_address='".$data['doctor_address']."',doctor_city='".$data['doctor_city']."',state_id='".$data['state_id']."',doctor_zipcode='".$data['doctor_zipcode']."',doctor_comments='".$data['doctor_comments']."',updated_date='".date("Y-m-d H:i:s")."' where doctor_id=".$data['doctor_id'];
		$result=$this->conn->dbCommand($query);		 */
		if($result !== 0){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function get_category($data)
	{
		$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->category_id = $data['category_id'];
		$result = DAOFactory::getDoctorsCategoryDAO()->load($doctorsCategory);
		//print_r($result);exit;
		/* $query="SELECT * from doctors_category
		WHERE category_id='".$data['category_id']."'";
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query); */		
		if($result !== ''){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}	
	
	}
	public function get_doctor($data)
	{
			$doctor = new Doctor();
		$doctor->doctor_id = $data['doctor_id'];
		$result = DAOFactory::getDoctorsDAO()->load($doctor);
		//print_r($result);exit;
		
		
	/* $query="SELECT d.*,dc.category_id,dc.category_name,st.state_id,st.state_name FROM doctors d left join doctors_category dc on d.category_id=dc.category_id left join states st on d.state_id=st.state_id where doctor_id='".$data['doctor_id']."'";
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query); */		
		if($result !== ''){
			return array('message'=>'Submodule type data','result'=>$result);			
		}else{
			return array('message'=>'Submodule by id is failed');
		}	
	
	}
	public function delete_category($data)
	{
		$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->category_id = $data['category_id'];
		$doctorsCategory->status = $data['status'];
		$result = DAOFactory::getDoctorsCategoryDAO()->delete($doctorsCategory);
		//print_r($result);exit;
		
		/* $query="update `doctors_category` set status='".$data['status']."' where category_id='".$data['category_id']."'";
		$data=$this->conn->dbCommand($query); */
		
		if($result !== 0)
			return array('message'=>"Category status changed Successfully");		
		else
			return array('message'=>'Category status changed failed');
	}
	public function delete_doctor($data)
	{
			$doctor = new Doctor();
		$doctor->doctor_id = $data['doctor_id'];
		$doctor->status = $data['status'];
		$result = DAOFactory::getDoctorsDAO()->delete($doctor);
		//print_r($result);exit;
		/* $query="update `doctors` set status='".$data['status']."' where doctor_id='".$data['doctor_id']."'";
		$data=$this->conn->dbCommand($query); */
		if($result !== 0)
			return array('message'=>"Submodule Deleted Successfully");		
		else
			return array('message'=>'Submodule Deletion Fails');
	}
	
	public function all_category($data){
		
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
/* 	$query_string="select * from doctors_category";
	$query_string1="select count(*) as total from doctors_category"; */
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		if($data['search_by'] == 'category_name')
		{	
			$search_by = "category_name";
			
		}
		if($data['search_by'] == 'admin_id')
		{
			$search_by = "admin_id";
			
		}
		if($data['search_by'] == 'created_date')
		{
			$search_by = "created_date";
		}
		
		$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->order_by = $order_by;
		$doctorsCategory->sort_order = $sort_order;
		$doctorsCategory->results_per_page = $results_per_page;
		$doctorsCategory->offset = $offset;
		$doctorsCategory->search_by = $search_by;
		$doctorsCategory->search_by_value = $data['search_by_value'];
		$result = DAOFactory::getDoctorsCategoryDAO()->queryAllSearchBy($doctorsCategory);
		$totalrows = DAOFactory::getDoctorsCategoryDAO()->SearchByCount($doctorsCategory);
		//print_r($totalrows);exit;
		
		/* $query = $query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 = $query_string1." where ".$search_by." like '%".$data['search_by_value']."%'"; */
		
	}else
	{
		$doctorsCategory = new DoctorsCategory();
		$doctorsCategory->order_by = $order_by;
		$doctorsCategory->sort_order = $sort_order;
		$doctorsCategory->results_per_page = $results_per_page;
		$doctorsCategory->offset = $offset;
		$result = DAOFactory::getDoctorsCategoryDAO()->queryAll($doctorsCategory);
		$totalrows = DAOFactory::getDoctorsCategoryDAO()->queryAllCount($doctorsCategory);
		//print_r($totalrows);exit;
		
		/* $query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 = $query_string1; */
	}	
		//$result=$this->conn->dbMultiple($query);
		if($result !== ''){
			//$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Doctors Categories');
		
	}
	
	public function all_doctors($data)
	{	
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	/* $query_string="SELECT d.*,dc.category_id,dc.category_name,st.state_id,st.state_name FROM doctors d left join doctors_category dc on d.category_id=dc.category_id left join states st on d.state_id=st.state_id";
	$query_string1="select count(*) as total
					FROM doctors d left join doctors_category dc on d.category_id=dc.category_id left join states st on d.state_id=st.state_id"; */
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		
		if($data['search_by'] == 'category_name')
		{
				$search_by = "dc.category_name";
		}else if($data['search_by'] == 'doctor_name')
		{
			$search_by = "doctor_name";
		}
			$doctor = new Doctor();
		$doctor->order_by = $order_by;
		$doctor->sort_order = $sort_order;
		$doctor->results_per_page = $results_per_page;
		$doctor->offset = $offset;
		$doctor->search_by = $search_by;
		$doctor->search_by_value = $data['search_by_value'];
		$result = DAOFactory::getDoctorsDAO()->queryAllSearchBy($doctor);
		$totalrows = DAOFactory::getDoctorsDAO()->SearchByCount($doctor);
		//print_r($totalrows);exit;
		
		/* $query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1 =$query_string1."where ".$search_by." like '%".$data['search_by_value']."%'"; */
		
	}else
	{
				$doctor = new Doctor();
		$doctor->order_by = $order_by;
		$doctor->sort_order = $sort_order;
		$doctor->results_per_page = $results_per_page;
		$doctor->offset = $offset;
		$result = DAOFactory::getDoctorsDAO()->queryAll($doctor);
		$totalrows = DAOFactory::getDoctorsDAO()->queryAllCount($doctor);
		//print_r($totalrows);exit;
		
	/* $query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1; */
	}	
		//$result=$this->conn->dbMultiple($query);
		if($result !== ''){
			//$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Doctors');
		
	}
}
	
?>